﻿#include"InFrame.h"




InFrame::InFrame(const wxString& title,std::vector<Json::Value>* draws)
    : wxFrame(nullptr, wxID_ANY, title, wxPoint(320, 350), wxSize(700, 433))
{
    pdraws = draws;
    panel = new wxPanel(this, wxID_ANY);

    wxBoxSizer* mainSizer = new wxBoxSizer(wxHORIZONTAL);

    listBox = new wxListBox(panel, wxID_ANY, wxPoint(320, 350), wxDefaultSize, 0, nullptr, wxLB_SINGLE);

    wxDir dir(wxT("元器件"));//扫描元器件
    if (dir.IsOpened())
    {
        wxString filename;
        bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES);
        while (cont)
        {
            selectedItems.push_back(filename);
            listBox->Append(filename);
            cont = dir.GetNext(&filename);
        }
    }

    // 绑定列表框的选择事件处理程序
    listBox->Bind(wxEVT_LISTBOX, &InFrame::OnListBoxSelected, this);
    mainSizer->Add(listBox, 1, wxEXPAND | 50, 10);

    rightPanel = new wxPanel(panel, wxID_ANY);
    rightPanel->SetBackgroundColour(wxColour(255, 255, 255));
    mainSizer->Add(rightPanel, 1, wxEXPAND | wxALL, 10);

    button = new wxButton(panel, IN_BUTTON, "确定");
    mainSizer->Add(button, 0, wxALL, 10);
    Connect(IN_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(InFrame::In));

    panel->SetSizer(mainSizer);

    rightPanel->Connect(wxEVT_PAINT, wxPaintEventHandler(InFrame::OnPaint), NULL, this);
}



void InFrame::OnListBoxSelected(wxCommandEvent& event)
{
    wxListBox* listBox = dynamic_cast<wxListBox*>(event.GetEventObject());
    int selectedIndex = listBox->GetSelection();

    wxString selectedItem = listBox->GetString(selectedIndex);
    wxString message;


    for (int i = 0; i < selectedItems.size(); i++) {
        if (selectedItem == selectedItems[i]) {
            draws.clear();
            std::string path = "元器件/";
            path += selectedItems[i].ToStdString();

            std::ifstream jsonFile(path);

            jsonFile >> drawing;
            jsonFile.close();

            for (auto& each : drawing) {
                draws.push_back(each);
            }


            rightPanel->Refresh();
        }
    }

   
}


void InFrame::OnPaint(wxPaintEvent& event)
{
    int x = 140;
    int y = 150;
    wxPaintDC dc(rightPanel);
    wxColor pencolor;
    int pensize = 2;
    wxColor brushcolor;


    for (int i = 0; i < draws.size(); ++i)
    {

        pencolor.SetRGB(draws[i]["pcrgb"].asInt());
        pensize = draws[i]["pensize"].asInt();
        wxPen pen2(pencolor, pensize);
        dc.SetPen(pen2);


        if (draws[i]["bcrgb"].asInt() == -1)//设置刷子
            dc.SetBrush(*wxTRANSPARENT_BRUSH);
        else
        {
            brushcolor.SetRGB(draws[i]["bcrgb"].asInt());
            wxBrush brush(brushcolor);
            dc.SetBrush(brush);
        }

        if (draws[i]["type"] == "line")
            dc.DrawLine(draws[i]["startx"].asInt()+x, draws[i]["starty"].asInt()+y,
                draws[i]["endx"].asInt()+x, draws[i]["endy"].asInt()+y);
        else if (draws[i]["type"] == "rect")
            dc.DrawRectangle({ draws[i]["startx"].asInt()+x, draws[i]["starty"].asInt()+y },
                { draws[i]["width"].asInt(), draws[i]["height"].asInt() });
        else if (draws[i]["type"] == "roud")
            dc.DrawCircle(draws[i]["centerx"].asInt()+x, draws[i]["centery"].asInt()+y, draws[i]["r"].asInt());
        else if (draws[i]["type"] == "arc")
            dc.DrawEllipticArc(draws[i]["startx"].asInt()+x, draws[i]["starty"].asInt()+y,
                draws[i]["width"].asInt(), draws[i]["height"].asInt(), 0, 180);
        else if (draws[i]["type"] == "point")
            dc.DrawCircle(draws[i]["centerx"].asInt() + x, draws[i]["centery"].asInt() + y, draws[i]["r"].asInt());
    }


}

void InFrame::In(wxCommandEvent& event) {

    (*pdraws).push_back(drawing);
    this->Close();
    
}
